package HongPro;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;

class Id extends JFrame implements ActionListener {
	static JTextField tf = new JTextField(8);
	JButton btn = new JButton("입력");

	WriteThread wt;
	ClientFrame cf;

	public Id() {
	}

	public Id(WriteThread wt, ClientFrame cf) {
		super("아이디");
		this.wt = wt;
		this.cf = cf;

		setLayout(new FlowLayout());
		add(new JLabel("아이디"));
		add(tf);
		add(btn);

		btn.addActionListener(this);

		setBounds(300, 300, 250, 100);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == btn) {
			wt.sendMsg();
			cf.isFirst = false;
			cf.setVisible(true);
			this.dispose();
		}
	}

	static public String getId() {
		return tf.getText();
	}
}

// ������ ������ ���� GUI ������
class FileTransfer extends JFrame implements ActionListener {
	JButton btn = new JButton("전송");
	JTextField tf = new JTextField(10);
	JPanel p = new JPanel();
	Socket socketFile;
	FileWrite fw;
	ClientFrame cf;

	public FileTransfer(ClientFrame cf) {
		this.cf = cf;

		try {
			socketFile = new Socket("127.0.0.1", 3003);
			new FileReadThread(socketFile).start();
		} catch (IOException ie) {
			System.out.println(ie.getMessage());
		}

		fw = new FileWrite(cf, socketFile);

		p.add(new JLabel("파일명"));
		p.add(tf);
		p.add(btn);
		add(p);

		btn.addActionListener(this);

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});

		setBounds(200, 200, 200, 200);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (tf.getText().equals("")) {
			return;
		}
		if (e.getSource() == btn) {

			fw.sendMsg(tf.getText());
			this.dispose();
		}
	}
}

public class ClientFrame extends JFrame implements ActionListener {
	JTextArea txtA = new JTextArea();
	
	JTextField txtF = new JTextField(15);
	JButton btnTransfer = new JButton("전송");
	JButton btnFileSend = new JButton("보내기");

	JButton btnExit = new JButton("닫기");
	boolean isFirst = true;
	JPanel p1 = new JPanel();
	Socket socket;
	WriteThread wt;
	private final JScrollPane scrollPane = new JScrollPane();

	public ClientFrame(Socket socket) {
		super("네이트온");
		this.socket = socket;
		wt = new WriteThread(this);

		new Id(wt, this);
				getContentPane().setLayout(new BorderLayout(0, 0));
		txtF.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				String id = Id.getId();
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					// 전송버튼 눌렀을 경우
					// 메세지 입력없이 전송버튼만 누른경우
					if (txtF.getText().equals("")) {
						return;
					}
					txtA.append("[" + id + "] " + txtF.getText() + "\n");
					wt.sendMsg();
					txtF.setText("");
				
				}
			}
		});
		p1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
				p1.add(txtF);
				p1.add(btnTransfer);
				p1.add(btnFileSend);
				p1.add(btnExit);
				getContentPane().add(p1, BorderLayout.SOUTH);
				getContentPane().add(scrollPane, BorderLayout.CENTER);
				scrollPane.setViewportView(txtA);
				
						btnFileSend.addActionListener(this);
						btnTransfer.addActionListener(this);
						btnExit.addActionListener(this);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(300, 300, 450, 300);
		setVisible(false);
	}

	public void actionPerformed(ActionEvent e) {
		txtA.setCaretPosition(txtA.getText().length());
		String id = Id.getId();
		if (e.getSource() == btnTransfer) {// 전송버튼 눌렀을 경우
			// 메세지 입력없이 전송버튼만 누른경우
			if (txtF.getText().equals("")) {
				return;
			}
			txtA.append("[" + id + "] " + txtF.getText() + "\n");
			wt.sendMsg();
			txtF.setText("");
		} else if (e.getSource() == btnExit) {// 닫기 버튼 누른경우
			try {
				if (socket != null)
					socket.close();
			} catch (IOException ie) {
				System.out.println(ie.getMessage());
			}

			this.dispose();
		} else if (e.getSource() == btnFileSend) {
			new FileTransfer(this);
		}
	}
}
